/*
* @Author: Administrator
* @Date:   2017-12-23 15:54:49
* @Last Modified by:   Administrator
* @Last Modified time: 2017-12-23 16:03:35
*/
import { observable, action } from 'mobx'
import { get } from '../util/comm'
import { newsDetailUrl } from '../util/constant';

const objToStrMap = (obj) => {
  const strMap = new Map();
  for (let k of Object.keys(obj)) { //eslint-disable-line
    strMap.set(k, obj[k]);
  }
  return strMap;
}
class NewsDetail {
  // 新闻分类页面的新闻数据
  @observable newsDetailData
  @observable isLoading
  constructor(initialState) {
    if (initialState && initialState.newsDetail) {
      if (initialState.newsDetail.newsDetailData) {
        this.newsDetailData = objToStrMap(initialState.newsDetail.newsDetailData)
        this.isLoading = initialState.newsDetail.isLoading
      } else {
        this.newsDetailData = new Map()
        this.isLoading = true
      }
    } else {
      this.newsDetailData = new Map()
      this.isLoading = true
    }
  }
  @action getDetailNews(id) {
    return new Promise((resolve, reject) => {
      if (!this.newsDetailData.get(id)) {
        const baseUrl = newsDetailUrl;
        const params = {
          id,
        }
        this.isLoading = true
        const key = id
        get(baseUrl, params).then((result) => {
          this.newsDetailData.set(key, result.data)
          this.isLoading = false
          resolve()
        }).catch(e => reject(e))
      } else {
        resolve()
      }
    })
  }
}
// autorun(() => {
//   console.log(111);
// })
export default NewsDetail
